/*
 * @Date: 2019-10-28 09:00:27
 * @LastEditors: 盛陈锋
 * @LastEditTime: 2020-05-12 09:57:31
 * @Author: 盛陈锋
 * @Company: 鑫合易家
 */

Component({
  options: {
    multipleSlots: true,
    addGlobalClass: true,
  },
  properties: {
    //弹窗标题
    title: {
      type: String,
      value: "",
    },
    //拓展样式
    extClass: {
      type: String,
      value: "",
    },
    //弹窗是否可以点击空白处关闭
    maskClosable: {
      type: Boolean,
      value: false,
    },
    mask: {
      type: Boolean,
      value: true,
    },
    show: {
      type: Boolean,
      value: false,
      observer: "_showChange",
    },
    //按钮
    buttons: {
      type: Array,
      value: [],
    },
    countdown: {
      type: Number,
      value: 0,
      observer: function (newVal) {
        if (newVal) {
          const _this = this;
          let currentTime = newVal;
          let interval = setInterval(function () {
            currentTime--;
            _this.setData({
              time: currentTime + "s",
              disabled: true,
            });
            if (currentTime <= 0) {
              clearInterval(interval);
              _this.setData({
                time: 0,
                disabled: false,
              });
            }
          }, 1000);
        }
      },
    },
  },
  //允许设置外部样式
  externalClasses: ["weui-dialog__wrp_2"],
  data: {
    innerShow: false,
    time: null,
    disabled: false,
  },
  ready: function ready() {
    var buttons = this.data.buttons;
    var len = buttons.length;
    buttons.forEach(function (btn, index) {
      if (len === 1) {
        btn.className = "weui-dialog__btn_primary";
      } else if (index === 0) {
        btn.className = "weui-dialog__btn_default";
      } else {
        btn.className = "weui-dialog__btn_primary";
      }
    });
    this.setData({
      buttons: buttons,
    });
  },

  methods: {
    buttonTap: function buttonTap(e) {
      var index = e.currentTarget.dataset.index;

      this.triggerEvent("buttontap", { index: index, item: this.data.buttons[index] }, {});
    },
    buttonTap2: function buttonTap(e) {
      if (this.data.time) {
        console.log(false);
        return;
      }
      console.log(true);
      var index = e.currentTarget.dataset.index;

      this.triggerEvent("buttontap", { index: index, item: this.data.buttons[index] }, {});
    },
    close: function close() {
      let data = this.data;
      if (!data.maskClosable) {
        return;
      }
      this.setData({
        show: false,
      });
      this.triggerEvent("close", {}, {});
    },
    stopEvent: function stopEvent() {},
  },
});
